package com.dianrui.agent.activity;

import android.widget.TextView;

import com.dianrui.agent.R;
import com.dianrui.agent.okhttp.OkHttpRequest;
import com.dianrui.agent.util.SharedUtil;
import com.dianrui.agent.util.StringUtils;
import com.dianrui.agent.util.ToastUtil;
import com.dianrui.agent.util.Url;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 七日化收益
 */
public class SevenDataAnalysisActivity extends BaseActivity {
    private List<String>xAxisValue=new ArrayList<>();
    private List<String> lineXvalues =new ArrayList<>();
    private List<Entry> yVals1All = new ArrayList<>();

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.line_chart)
    LineChart lineChart;//线性图


    @Override
    public int getContentViewId() {
        return R.layout.seven_analysis;
    }

    @Override
    public void initView() {
        title.setText("数据分析");
        getEarings();
    }

    private void getEarings() {
        OkHttpRequest.getInstance().postTokenRequests(SharedUtil.getUrl()+Url.SEVEN_MONEY, "",  new OkHttpRequest.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if ("1000".equals(jsonObject.optString("status"))) {
                            JSONArray consume =jsonObject.optJSONObject("data").optJSONArray("consume");
                            if (yVals1All != null) {
                                yVals1All.clear();
                            }
                            if (consume.length()>0) {
                                List<Entry> yVals1 = new ArrayList<>();
                                yVals1.add(new Entry(0f,StringUtils.toFloat(consume.get(0))));
                                yVals1.add(new Entry(1f,StringUtils.toFloat(consume.get(1))));
                                yVals1.add(new Entry(2f,StringUtils.toFloat(consume.get(2))));
                                yVals1.add(new Entry(3f,StringUtils.toFloat(consume.get(3))));
                                yVals1.add(new Entry(4f,StringUtils.toFloat(consume.get(4))));
                                yVals1.add(new Entry(5f,StringUtils.toFloat(consume.get(5))));
                                yVals1.add(new Entry(6f,StringUtils.toFloat(consume.get(6))));
                                yVals1All.addAll(yVals1);

                            }
                            List<Double> yNumber = new ArrayList<>();
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(0))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(1))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(2))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(3))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(4))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(5))));
                            yNumber.add(Double.valueOf(String.valueOf(consume.get(6))));
                            Double maxyNumber = Collections.max(yNumber);

                            JSONArray xAxis=jsonObject.optJSONObject("data").optJSONArray("xAxis");
                            if (lineXvalues != null) {
                                lineXvalues .clear();
                            }
                            if (xAxis.length()>0) {
                                for (int i = 0; i <xAxis.length() ; i++) {
                                    String str = xAxis.optString(i);
                                    lineXvalues .add(str);
                                }
                            }
                            initLineChart(lineXvalues ,yVals1All,StringUtils.toFloat(maxyNumber));


                        }else if ("1001".equals(jsonObject.optString("status"))) {
                            ToastUtil.showToast(jsonObject.optString("message"));
                        } else if ("1002".equals(jsonObject.optString("status"))) {
                            spUtils.clear();
                            ToastUtil.showToast(jsonObject.optString("message"));
                            JumpActivitys(LoginActivity.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void FailedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }

    //折线图
    private void initLineChart(List<String> lineX, List<Entry> yVals1All,float ymaxNumber) {
        LineDataSet dataSet = new LineDataSet(yVals1All, "");
        XAxis xAxis = lineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLabels(true);
        xAxis.setGranularity(1f);//设置x轴的间距
        xAxis.setLabelCount(lineX.size());
        xAxis.setAxisLineWidth(1f);//设置x轴的宽度
        xAxis.setTextSize(4f);
        xAxis.setValueFormatter(new IndexAxisValueFormatter(lineX));//设置X轴标签的文字
        lineChart.setTouchEnabled(false);
        lineChart.getAxisRight().setEnabled(false);//右侧y轴屏蔽
        YAxis leftAxis = lineChart.getAxisLeft();
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.enableGridDashedLine(10f, 10f, 0f);
        leftAxis.setSpaceTop(9f);
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(ymaxNumber+1900f);
        leftAxis.setDrawZeroLine(false);
        Legend l = lineChart.getLegend();
        l.setEnabled(false);
        Description description = new Description();
        description.setEnabled(false);
        lineChart.setDescription(description);

        LineData lineData = new LineData(dataSet);
        lineChart.setData(lineData);
        lineChart.invalidate();

    }



    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (lineXvalues != null) {
            lineXvalues.clear();
        }
        if (yVals1All != null) {
            yVals1All.clear();
        }
    }
}
